import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';
import type { App, Plugin } from 'vue';

import { unref } from 'vue';
import { isObject, isEmpty } from './EsIs';

export const noop = () => { };

/**
 * @description:  Set ui mount node
 */
export function getPopupContainer(node?: HTMLElement): HTMLElement {
  return (node?.parentNode as HTMLElement) ?? document.body;
}

/**
 * Add the object as a parameter to the URL
 * @param baseUrl url
 * @param obj
 * @returns {string}
 * eg:
 *  let obj = {a: '3', b: '4'}
 *  setObjToUrlParams('www.baidu.com', obj)
 *  ==>www.baidu.com?a=3&b=4
 */
export function setObjToUrlParams(baseUrl: string, obj: any): string {
  let parameters = '';
  for (const key in obj) {
    parameters += key + '=' + encodeURIComponent(obj[key]) + '&';
  }
  parameters = parameters.replace(/&$/, '');
  return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;
}

export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
  let key: string;
  for (key in target) {
    src[key] = isObject(src[key]) ? deepMerge(src[key], target[key]) : (src[key] = target[key]);
  }
  return src;
}

export function openWindow(
  url: string,
  opt?: { target?: string; noopener?: boolean; noreferrer?: boolean },
) {
  const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
  const feature: string[] = [];

  noopener && feature.push('noopener=yes');
  noreferrer && feature.push('noreferrer=yes');

  window.open(url, target, feature.join(','));
}

export function locationHref(url: string) {
  window.location.href = url
}



export function getUrlKey(name, href = "") {
  if (href === "") href = window.location.href
  let arr: any = new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(
    href,
  ) || [, ''];
  let urlValue = decodeURIComponent(arr[1].replace(/\+/g, '%20')) || null;
  if (urlValue == null) {
    name = name.toLowerCase();
    urlValue = decodeURIComponent(arr[1].replace(/\+/g, '%20')) || null;
  }
  return urlValue;
}
export function getNewObj(val) {
  let resObj = JSON.stringify(val);
  return JSON.parse(resObj);
}

//列表value显示名称
export function findValue(Options, value:any) {
  const obj = Options.find(item => item.value == value)
  if (obj) {
    return obj.label
  } else {
    return ""
  }
}
export function mapValue(arry) {
  let arryList = Object.keys(arry);
  let list = <any>[]
  arryList.forEach((key) => {
    list.push(
      {
        text: arry[key],
        label: arry[key],
        value: key,
        key: key,
      }
    )
  });
  return list
}
/**
 * @name: 处理公共参数列表
 * @msg:
 * @param {*} arry
 * @return {*}
 */
export function enumValue(res: any) {
  const arr: any = res.map((item) => {
    return {
      label: item.enumDesc,
      value: item.enumNo,
      title: item.enumDesc,
      key: item.enumNo,
      isCheck: false,
      ...item,
    };
  });
  return arr
}
/**
 * @name: 客户阶段
 * @msg:
 * @param {*} arry
 * @return {*}
 */
export function stageValue(res: any) {
  const arr: any = res.map((item) => {
    return {
      label: item.name,
      value: item.id,
      title: item.name,
      key: item.id,
      isCheck: false,
      ...item,
    };
  });
  return arr
}

